package com.china08.yxyapi.api.manage.stu.mystudy.tifenbao.scoreimprove;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.huixue.YxyHuixuePlan;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.huixue.YxyHuixuePlanRepository;

import io.swagger.annotations.ApiOperation;

/**
 * 首页--学习计划(学习计划列表)
 * 
 */
@CrossOrigin
@RestController
public class TifenbaoPlanLstController extends YxyApiBaseController {
    @Autowired
    private YxyHuixuePlanRepository yxyHuixuePlanRepository;

    @ApiOperation(value = "首页 学习计划列表")
    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/stu/mystudy/tifenbao/scoreimprove/lst", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) RepModel reqModel) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Pageable pageable = reqModel.getPageable(Direction.DESC, "createdDate");
        Page<YxyHuixuePlan> page = yxyHuixuePlanRepository.findByStudentIdAndCurrentScore(reqModel.studentId, -1d,
                pageable);
        List<RespModel> list = new ArrayList<TifenbaoPlanLstController.RespModel>();
        for (YxyHuixuePlan yxyHuixuePlan : page) {
            RespModel model = new RespModel();
            model.id = yxyHuixuePlan.getId();
            model.studentId = yxyHuixuePlan.getStudentId();
            model.subjectName = yxyHuixuePlan.getSubjectName();
            model.versionName = yxyHuixuePlan.getVersionName();
            model.cebieName = yxyHuixuePlan.getSemesterName();
            model.bookContentName = yxyHuixuePlan.getBookContentName();
            model.lastScore = yxyHuixuePlan.getLastScore();
            model.expectScore = yxyHuixuePlan.getExpectScore();
            // 开始时间
            Integer beginDate = yxyHuixuePlan.getBeginDate();
            String beginDateString = null;
            if (beginDate != null && beginDate != 0) {
                beginDateString = String.valueOf(beginDate);
                beginDateString = beginDateString.substring(0, 4) + "-" + beginDateString.substring(4, 6) + "-"
                        + beginDateString.substring(6);
            }
            // 结束时间
            Integer endDate = yxyHuixuePlan.getEndDate();
            String endDateString = null;
            if (endDate != null && endDate != 0) {
                endDateString = String.valueOf(endDate);
                endDateString = endDateString.substring(0, 4) + "-" + endDateString.substring(4, 6) + "-"
                        + endDateString.substring(6);
            }
            Date endDate2 = sdf.parse(endDateString);
            // 如果计划是已截止状态剩余时间是0
            model.resttime = yxyHuixuePlan.getStatus() == 1
                    ? (getIntervalDays(sdf.parse(sdf.format(new Date())), endDate2) + 1) : 0;
            model.status = yxyHuixuePlan.getStatus();
            int usetime = this.getIntervalDays(sdf.parse(beginDateString), endDate2) + 1;// 学习计划用时
            if (yxyHuixuePlan.getStatus() == 1 && (model.resttime <= 2 || model.resttime * 1d / usetime <= 0.2)) {
                model.timecolor = 1;
            } else {
                model.timecolor = 2;
            }
            list.add(model);
        }
        Page<RespModel> respModel = new PageImpl<TifenbaoPlanLstController.RespModel>(list, pageable,
                page.getTotalElements());
        return respModel;

    }

    protected static class RepModel extends YxyApiPageReqModel {

        public String studentId;// 学生id

    }

    protected static class RespModel extends YxyApiRespModel {
        public String id;// 计划id

        public String studentId;// 学生id

        public String subjectName;// 学科名称

        public String versionName;// 版本名称

        public String cebieName;// 册别名称

        public String bookContentName;// 章

        public Double lastScore;// 初始分数

        public Integer expectScore;// 预期提高分数: 5, 10

        public Integer resttime;// 距离结束时间(0表示计划结束)

        public Integer timecolor;// 时间颜色 1：红色

        public Integer status;// 学习计划的状态：0--已截止 , 1--进行中

    }

    private Integer getIntervalDays(Date fDate, Date oDate) {

        if (null == fDate || null == oDate) {

        return -1;

        }

        long intervalMilli = oDate.getTime() - fDate.getTime();

        return (int) (intervalMilli / (24 * 60 * 60 * 1000));

    }

}
